﻿using System;
using System.Collections;
using System.Collections.Generic;
using Letspirate.Utilities.Infrastructure.Collections;

namespace Letspirate.Utilities.Collections
{
    public class UniqueList<T> : IUniqueList<T>
    {
        private readonly IList<T> _collection;

        public UniqueList()
        {
            _collection = new List<T>();
        }

        #region Implementation of IEnumerable

        public IEnumerator<T> GetEnumerator()
        {
            return _collection.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion

        #region Implementation of IUniqueList<T>

        public void Add(T item)
        {
            if (_collection.Contains(item))
                throw new ArgumentException("Can't add a duplicate item");

            _collection.Add(item);
        }

        public bool TryAdd(T item)
        {
            if (_collection.Contains(item))
                return false;

            _collection.Add(item);

            return true;
        }

        public bool Remove(T item)
        {
            return _collection.Remove(item);
        }

        #endregion
    }

}